package com.woniu.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.exception.order.OrderException;
import com.woniu.exception.order.OrderExceptionCode;
import com.woniu.order.client.FlowClient;
import com.woniu.order.cliententity.form.AddHandelForm;
import com.woniu.order.mapper.OrderCommentMapper;
import com.woniu.order.mapper.TOrderMapper;
import com.woniu.order.model.OrderComment;
import com.woniu.order.model.TOrder;
import com.woniu.order.param.AddUserCommentParam;
import com.woniu.order.service.OrderCommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@Service
public class OrderCommentServiceImpl extends ServiceImpl<OrderCommentMapper, OrderComment> implements OrderCommentService {

    @Resource
    private OrderCommentMapper orderCommentMapper;

    @Resource
    private TOrderMapper orderMapper;

    @Resource
    private FlowClient flowClient;

    /**
     * 用户评价
     * @param toBean 订单号 用户评价 用户评星
     * @param userId 用户id
     * @return 评论对象
     */
    @Override
    public OrderComment userComment(AddUserCommentParam toBean, Integer userId) {
        TOrder tOrder = orderMapper.selectById(toBean.getOrderNum());
        if (tOrder==null||tOrder.getUserId()!=userId.longValue()){
            throw new OrderException(OrderExceptionCode.ORDER_EXCEPTION_CODE_NO_ORDER);
        }
        OrderComment orderComment = new OrderComment(null, toBean.getOrderNum(), tOrder.getState(), toBean.getComment(), null, toBean.getStar());
        orderCommentMapper.insert(orderComment);
        AddHandelForm addHandelForm = new AddHandelForm(null, 9, null, toBean.getOrderNum(), null);
        flowClient.add(addHandelForm);
        return orderComment;
    }

}
